<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Controllers\utils;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ImportController extends Controller {
    use utils;

    public function codData(Request $request) {
        $forms = $request->input('cod_datas', []);
        $codFields = ['customer', 'order_account', 'sub_account_name', 'delivered_date', 'tracking_no', 'cod_amount'];
        if (!is_array($forms)) {
            return $this->json_msg(500, '', '请上传数据请求格式错误');
        }
        if (count($forms) <= 0) {
            return $this->json_msg(500, '', '请上传数据');
        }
        if (count($forms) > 200) {
            return $this->json_msg(500, '', '一次最多上传200条数据');
        }
        $datas = [];
        foreach ($forms as $form) {
            $lineData = [];
            foreach ($codFields as $field) {
                if (isset($form[$field])) {
                    $lineData[$field] = $form[$field];
                } else {
                    $lineData[$field] = null;
                }
            }
            if (count($lineData)) {
                $now = $this->now();
                $lineData['updated_at'] = $now;
                $lineData['created_at'] = $now;
                array_push($datas, $lineData);
            }
        }
        if (count($datas) > 0) {
            DB::beginTransaction();
            try {
                $result = DB::table('cod')
                    ->insert($datas);
            } catch (\Exception $ex) {
                DB::rollBack();
                return $this->json_msg(500, '', '保存异常：'.$ex->getMessage());
            }
            if ($result) {
                DB::commit();
                return $this->json_msg(200, '', '保存成功');
            } else {
                return $this->json_msg(500, '', '保存失败');
            }
        } else {
            return $this->json_msg(500, '', '没有可导入的数据');
        }
    }

    public function pickupOrdersData(Request $request) {
        $forms = $request->input('order_datas', []);
        $orderFields = [
            'date',
            'pu_time',
            'operated_by',
            'sub_account_name',
            'source',
            'parcel_type',
            'order_no',
            'tracking_no',
            'original_tracking_no',
            'return_tracking_no',
            'status',
            'sender',
            'sender_phone',
            'sender_address',
            'consignee',
            'consignee_phone',
            'consignee_phone2',
            'consignee_address',
            'item_type',
            'remark1',
            'remark2',
            'remark3',
            'weight',
            'size',
            'chargeable_weight',
            'product_type',
            'estimated_delivery_time',
            'standard_shipping_fee',
            'packaging_fee',
            'cod_fee',
            'cod_transfer_fee',
            'label_cost',
            'ka_discount',
            'ordinary_discount',
            'total_charge',
            'cod_amt',
            'delivery_time',
            'signer',
            'signer_type'
        ];
        if (!is_array($forms)) {
            return $this->json_msg(500, '', '请上传数据请求格式错误');
        }
        if (count($forms) <= 0) {
            return $this->json_msg(500, '', '请上传数据');
        }
        if (count($forms) > 200) {
            return $this->json_msg(500, '', '一次最多上传200条数据');
        }
        $datas = [];
        foreach ($forms as $form) {
            $lineData = [];
            foreach ($orderFields as $field) {
                if (isset($form[$field])) {
                    $lineData[$field] = $form[$field];
                } else {
                    $lineData[$field] = null;
                }
            }
            if (count($lineData)) {
                $now = $this->now();
                $lineData['updated_at'] = $now;
                $lineData['created_at'] = $now;
                array_push($datas, $lineData);
            }
        }
        if (count($datas) > 0) {
            DB::beginTransaction();
            try {
                $result = DB::table('pickup_orders')
                    ->insert($datas);
            } catch (\Exception $ex) {
                DB::rollBack();
                return $this->json_msg(500, '', '保存异常：'.$ex->getMessage());
            }
            if ($result) {
                DB::commit();
                return $this->json_msg(200, '', '保存成功');
            } else {
                return $this->json_msg(500, '', '保存失败');
            }
        } else {
            return $this->json_msg(500, '', '没有可导入的数据');
        }
    }

    public function returnData(Request $request) {
        $forms = $request->input('return_datas', []);
        $orderFields = [
            'date',
            'transaction_no',
            'tracking_no',
            'type',
            'dst_name',
            'dst_detail_address',
            'status',
            'freight_insurance1',
            'origin_branch',
            'destination_branch1',
            'current_parcel_located_branch',
            'destination_branch2',
            'chargeable_weight_kg',
            'act_received_freight',
            'on_time_delivery',
            'box_shield_tax_included',
            'label_cost_when_picked_up',
            'packaging_fee',
            'flash_care',
            'cod_fee',
            'cod_transfer_fee',
            'freight_insurance2',
            'total_amount',
            'label_cost_when_returned',
            'return_fee',
            'total_return_fee',
            'cod_amount',
            'booking_time',
            'picked_up_time',
            'delivery_time',
            'origin_courier',
            'destination_courier',
            'duration_from_booking_to_picking_up',
            'duration_from_picking_up_to_delivery',
            'freight_billing_method',
            'payment_method'
        ];
        if (!is_array($forms)) {
            return $this->json_msg(500, '', '请上传数据请求格式错误');
        }
        if (count($forms) <= 0) {
            return $this->json_msg(500, '', '请上传数据');
        }
        if (count($forms) > 200) {
            return $this->json_msg(500, '', '一次最多上传200条数据');
        }
        $datas = [];
        foreach ($forms as $form) {
            $lineData = [];
            foreach ($orderFields as $field) {
                if (isset($form[$field])) {
                    $lineData[$field] = $form[$field];
                } else {
                    $lineData[$field] = null;
                }
            }
            if (count($lineData)) {
                $now = $this->now();
                $lineData['updated_at'] = $now;
                $lineData['created_at'] = $now;
                array_push($datas, $lineData);
            }
        }
        if (count($datas) > 0) {
            DB::beginTransaction();
            try {
                $result = DB::table('return_data')
                    ->insert($datas);
            } catch (\Exception $ex) {
                DB::rollBack();
                return $this->json_msg(500, '', '保存异常：'.$ex->getMessage());
            }
            if ($result) {
                DB::commit();
                return $this->json_msg(200, '', '保存成功');
            } else {
                return $this->json_msg(500, '', '保存失败');
            }
        } else {
            return $this->json_msg(500, '', '没有可导入的数据');
        }
    }
}
